﻿using System;
using Microsoft.Office.Server.Audience;
using Microsoft.SharePoint;
using System.Collections;
using SCCL2010;
namespace SCCL2010Server
{
    public static class Audiences
    {

        private static string GetAudienceID(SPWeb oWeb, string cAudienceName)
        {
            return "";
        }

        /// <summary>
        /// Method to check if the audience exists.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <returns>returns a boolean value if the Audience exists or not.</returns>
        public static bool HasAudience(string cAudienceName)
        {
            return HasAudience(SPContext.Current.Web, cAudienceName);
        }

        /// <summary>
        /// Method to check if the audience exists.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <returns>returns a boolean value if the Audience exists or not.</returns>
        public static bool HasAudience(SPWeb oWeb, string cAudienceName)
        {
            try
            {
                using (SPSite site = oWeb.Site)
                {
                    SPServiceContext context = SPServiceContext.Current;
                    AudienceManager audManager = new AudienceManager(context);
                    return audManager.Audiences.AudienceExist(cAudienceName);
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "Audiences.HasAudience", ex);
            }
            return false;
        }

        /// <summary>
        /// Add Audience to the current site.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <param name="cAudienceDescription"></param>
        public static void AddAudience(string cAudienceName, string cAudienceDescription)
        {
            AddAudience(SPContext.Current.Web, cAudienceName, cAudienceDescription);
        }

        /// <summary>
        /// Add Audience to the current site.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <param name="cAudienceDescription"></param>
        public static void AddAudience(SPWeb oWeb, string cAudienceName, string cAudienceDescription)
        {
            try
            {
                AudienceManager audManager = new AudienceManager(SPServiceContext.Current);
                Audience oAud = audManager.Audiences.Create(cAudienceName, cAudienceDescription, AudienceGroupOperation.AUDIENCE_OR_OPERATION);
                ArrayList audienceRules = new ArrayList();
                AudienceRuleComponent r1 = new AudienceRuleComponent("AccountName", "Contains", oWeb.CurrentUser.LoginName);
                audienceRules.Add(r1);
                oAud.AudienceRules = audienceRules;
                oAud.Commit();
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "Audiences.AddAudience", ex);
            }
            
        }

        /// <summary>
        /// Method to check if the AudienceMember exists.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <param name="cMemberName"></param>
        /// <returns>returns a boolean value representing whether the MemberName exits.</returns>
        public static bool HasAudienceMember(string cAudienceName, string cMemberName)
        {
            return HasAudienceMember(SPContext.Current.Web, cAudienceName, cMemberName);
        }

        /// <summary>
        /// Method to check if the AudienceMember exists.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cAudienceName"></param>
        /// <param name="cMemberName"></param>
        /// <returns>returns a boolean value representing whether the MemberName exits.</returns>
        public static bool HasAudienceMember(SPWeb oWeb, string cAudienceName, string cMemberName)
        {
            try
            {


                AudienceManager audManager = new AudienceManager(SPServiceContext.Current);
                Audience oAud = audManager.GetAudience(cAudienceName);
                foreach (AudienceRuleComponent rule in oAud.AudienceRules)
                {
                    if (rule.RightContent == cMemberName)
                        return true;
                }

            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "Audiences.HasAudienceMember", ex);
            }
            return false;
        }

        /// <summary>
        /// Add Audience Member to the current site.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <param name="cMemberName"></param>
        public static void AddAudienceMember(string cAudienceName, string cMemberName)
        {
            AddAudienceMember(SPContext.Current.Web, cAudienceName, cMemberName);
        }

        /// <summary>
        /// Add Audience Member to the current site.
        /// </summary>
        /// <param name="cAudienceName"></param>
        /// <param name="cMemberName"></param>
        public static void AddAudienceMember(SPWeb oWeb, string cAudienceName, string cMemberName)
        {
            if (!HasAudienceMember(oWeb, cAudienceName, cMemberName))
            {
                try
                {
                    AudienceManager audManager = new AudienceManager(SPServiceContext.Current);
                    Audience oAud = audManager.GetAudience(cAudienceName);
                    ArrayList audienceRules = oAud.AudienceRules;
                    AudienceRuleComponent r2 = new AudienceRuleComponent(null, "OR", null);
                    audienceRules.Add(r2);
                    AudienceRuleComponent r1 = new AudienceRuleComponent("AccountName", "Contains", cMemberName);
                    oAud.GroupOperation = AudienceGroupOperation.AUDIENCE_OR_OPERATION;
                    audienceRules.Add(r1);
                    oAud.AudienceRules = audienceRules;
                    oAud.Commit();

                }
                catch (Exception ex)
                {
                    ErrorHandling.HandleError(oWeb, "Audiences.AddAudienceMember", ex);
                }
            }

        }



    }
}
